package avicit.bdp.dcs.log.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.dto.TaskInstanceDto;
import avicit.bdp.common.service.dto.TaskSpeedDTO;
import avicit.bdp.dcs.log.dao.TaskSpeedDao;
import avicit.bdp.dcs.log.dto.JobLogDTO;
import avicit.bdp.dcs.log.dto.TaskLogDetailDTO;
import com.github.pagehelper.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-08-07 11:02
 * @类说明：
 * @修改记录：
 */
@Service
@Transactional
public class DcsTaskLogService extends BaseService<TaskSpeedDao, TaskSpeedDTO> {

    private final TaskSpeedDao taskSpeedDao;

    @Autowired
    public DcsTaskLogService(TaskSpeedDao taskSpeedDao) {
        this.taskSpeedDao = taskSpeedDao;
    }

    public TaskSpeedDTO getTaskSpeedByTaskId(String taskId) {
        return taskSpeedDao.getTaskSpeedByTaskId(taskId);
    }

    public Page<TaskSpeedDTO> getHistoryTaskSpeedListByProcessInstanceId(String processInstanceId) {
        return taskSpeedDao.getHistoryTaskSpeedListByProcessInstanceId(processInstanceId);
    }

    public Page<TaskSpeedDTO> getHistorySparkTaskListByProcessInstanceId(String processInstanceId) {
        return taskSpeedDao.getHistorySparkTaskListByProcessInstanceId(processInstanceId);
    }

    public Map<String, Long> getSourceDiffListByProcessInstance(List<String> processInstanceList){
        Map<String, Long> map = new HashMap<>();
        List<Map<String, Object>> list = taskSpeedDao.getSourceDiffListByProcessInstance(processInstanceList);
        if (CollectionUtils.isNotEmpty(list)) {
            for(Map<String, Object> item : list){
                String key = (String) item.get("process_instance_id");
                Object obj = item.get("source_diff");
                if(obj == null){
                    map.put(key, 0L);
                }else{
                    map.put(key, Long.valueOf(obj.toString()));
                }
            }
        }

        return map;
    }

    public Long selectCountByTaskInstanceId(String taskInstanceId, String startTime, String endTime) {
        return taskSpeedDao.selectCountByTaskInstanceId(taskInstanceId, startTime, endTime);
    }

    public List<TaskLogDetailDTO> getPerformanceByTaskInstanceId(String taskInstanceId, String startTime, String endTime) {
        return taskSpeedDao.getPerformanceByTaskInstanceId(taskInstanceId, startTime, endTime);
    }

    public List<TaskLogDetailDTO> getIntervalPerformanceByTaskInstanceId(String taskInstanceId, String startTime, String endTime, Integer interval) {
        return taskSpeedDao.getIntervalPerformanceByTaskInstanceId(taskInstanceId, startTime, endTime, interval);
    }

    public Page<JobLogDTO> getHistoryJobLogListByJobId(String processInstanceId) {
        return taskSpeedDao.getHistoryJobLogListByJobId(processInstanceId);
    }

}
